package com.itqf.Service.Requestion.Impl;
/**
 * 大帅 2019/10/14 21:21
 */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.Dao.RequestionMapper;
import com.itqf.Dao.RequestionanswerMapper;
import com.itqf.Service.Requestion.RequestionService;
import com.itqf.entity.Requestion;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RequestionServiceImpl implements RequestionService {

    @Resource
    private RequestionMapper requestionMapper;
    @Resource
    private RequestionanswerMapper requestionanswerMapper;


    @Override
    public PageInfo<Requestion> findall(int pageindex, int pagesize,String requestiontitle)
    {
        PageHelper.startPage(pageindex, pagesize);
        Map map=new HashMap();
        map.put("requestiontitle",requestiontitle);
        List<Requestion> requestionList = requestionMapper.findall(map);
        return new PageInfo(requestionList);
    }

    @Override
    @Transactional
    public int insert(Requestion requestion) {
        return requestionMapper.insert(requestion);
    }

    /** 
    * @Description: 用户提问的删除
    * @Param: [requestionid, userid] 
    * @return: int 
    * @Author: 陈岗阳
    * @Date: 2019/10/20 
    */ 
    @Override
    @Transactional
    public int delete(int requestionid,String userid) {
        Map map=new HashMap();

        map.put("requestionId",requestionid);
        map.put("requestionUserid",userid);
        int count=0;
//        删除提问表的数据
        final int delete = requestionMapper.delete(map);
//删除提问中间表的数据
        final int i = requestionanswerMapper.deleteByPrimaryKey(requestionid);

        if (delete>0&&i>0)
        {
            count++;
        }

        return count;
    }

    @Override
    public int updateByPrimaryKey(Requestion requestion) {
        return requestionMapper.updateByPrimaryKey(requestion);
    }

    @Override
    public List<Requestion> findbyname(String requestiontitle) {
        List<Requestion> requestionList = requestionMapper.findbyname(requestiontitle);
        return requestionList;
    }

    /** 
    * @Description: 用户提问增加
    * @Param: [originalFilename, userid, answerTitle] 
    * @return: int 
    * @Author: 陈岗阳
    * @Date: 2019/10/20 
    */ 
    @Override
    public int insertrequestion(String originalFilename, String userid, String requestionTitle)
    {
        Map map=new HashMap();
        map.put("requestionUserid",userid);
        map.put("requestionTitle",requestionTitle);
        map.put("answerquestions",originalFilename);

        return requestionMapper.insertrequestion(map);
    }

    /** 
    * @Description: 用户提问的修改 
    * @Param: [originalFilename, userid, answerTitle, requestionId] 
    * @return: int 
    * @Author: 陈岗阳
    * @Date: 2019/10/20 
    */ 
    @Override
    public int updateAnserer(String originalFilename, String userid, String answerTitle, int requestionId) {
        Map map=new HashMap();
        map.put("answerquestions",originalFilename);
        map.put("requestionUserid",userid);
        map.put("requestionTitle",answerTitle);
        map.put("requestionId",requestionId);

        return requestionMapper.updateAnserer(map);
    }
}

